﻿#include "transformation.h"
#include "../../_global.h"
#include "../containerscommon.h"

#ifdef _DEBUG
//#ifdef _CRTDBG_MAP_ALLOC
#define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
//#endif 
#endif 


void Transformation::init(){
  flags = 0x1 | 0x2 | 0x4;
}

Transformation::Transformation():DataContainerBase(){
	init();
}

Transformation::Transformation(__int64 _ID):DataContainerBase(_ID){
  init();
}

Transformation::~Transformation(){
	
}

long Transformation::parse(SPtr<FileContainer> _inFileContainer,long _offsetInData){

	long dataSize;
	char *dataBuf=NULL;

  if(preParse(_inFileContainer,_offsetInData,&dataBuf,&dataSize)!=1)return -1;
	if(dataBuf==NULL)return -5;

  //Разбор контейнера для конкретного типа (этого объекта):

	setID(getFileContainer().p()->getID());
	long offset=0;

	if(dataSize<48)return -4;
	
	memcpy((void *)(&flags),(void *)(dataBuf+offset),4);
	offset+=4;
	memcpy((void *)(&pshx),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&pshy),(void *)(dataBuf+offset),4);
	offset+=4;
	memcpy((void *)(&prx),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&pry),(void *)(dataBuf+offset),4);
	offset+=4;
	memcpy((void *)(&clip_pshx),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&clip_pshy),(void *)(dataBuf+offset),4);
	offset+=4;
	memcpy((void *)(&clip_plx),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&clip_ply),(void *)(dataBuf+offset),4);
	offset+=4;
	memcpy((void *)(&scaleX),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&scaleY),(void *)(dataBuf+offset),4);	
	offset+=4;
	memcpy((void *)(&rotateAngle),(void *)(dataBuf+offset),4);	
	offset+=4;

	return offset;

}

long Transformation::parse(){
	if(getFileContainer().isNULLPtr())return -1;
	if(getFileContainer().p()->getContainerType()!=CT_TRANSFORMATION)return -3;
	return parse(getFileContainer(),0);
}

int Transformation::join(){

	if(preJoin()!=1)return -1;
	if(checkAndSetObjectsAtIDs()<0)return -2;

  //сборка данных в контейнер:
	getFileContainer().p()->setContainerType(CT_TRANSFORMATION);
	getFileContainer().p()->setID(getID());
	
	char* data;
	long size;
	
  if(sizeof(float)!=4)return -3;

	size=48;
	data=new char[size];
	((unsigned long *)data)[0]=flags;
	((float *)data)[1]=pshx;
	((float *)data)[2]=pshy;
	((float *)data)[3]=prx;
	((float *)data)[4]=pry;
	((float *)data)[5]=clip_pshx;
	((float *)data)[6]=clip_pshy;
	((float *)data)[7]=clip_plx;
	((float *)data)[8]=clip_ply;
	((float *)data)[9]=scaleX;
	((float *)data)[10]=scaleY;
	((long *)data)[11]=rotateAngle;
	getFileContainer().p()->setDataCopy(data,size);
	delete [] data;
	data=NULL;

	return 0;

}

int Transformation::checkAndSetObjectsAtIDs(){
  return 0;
}




